<?php

declare(strict_types=1);

namespace App\Service;

use App\Model\SysFile as SysFileModel;
use App\Model\SysFileGroup as SysFileGroupModel;

class SysFile extends BaseService
{
    // 分组列表
    public function groupList($map)
    {
        return SysFileGroupModel::query()->where($map)->orderBy('sort', 'desc')->get();
    }

    // 添加分组
    public function addGroup($data)
    {
        return SysFileGroupModel::insert($data);
    }

    // 编辑分组
    public function editGroup($group_id, $data)
    {
        return SysFileGroupModel::query()->where('group_id', $group_id)->update($data);
    }

    // 删除分组
    public function deleteGroup($group_id)
    {
        $count = SysFileModel::where(['group_id' => $group_id])->count();
        if ($count > 0) {
            $this->returnError('该分组下存在资源，无法删除');
        }
        return SysFileGroupModel::where('group_id', $group_id)->delete();
    }

    // 保存文件
    public function fileSave($data)
    {
        return SysFileModel::insert($data);
    }

    // 文件分页列表
    public function list(array $map, int $page = 16)
    {
        return SysFileModel::query()->where($map)->orderBy('file_id', 'desc')->paginate($page);
    }
}
